package uk.co.cocking.runner;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.io.StringWriter;

public class CommandRunner {

	private StreamConsumer sysErr, sysOut;

	public int run(String command) throws InterruptedException, IOException {
		Process process = Runtime.getRuntime().exec(command);
		sysErr = new StreamConsumer(process.getErrorStream());
		sysOut = new StreamConsumer(process.getInputStream());
		return process.waitFor();
	}

	public String sysErr() {
		return sysErr.toString();
	}
	
	public String sysOut() {
		return sysOut.toString();
	}

	class StreamConsumer implements Runnable {
		private InputStream inputStream;
		private StringWriter stringWriter = new StringWriter();
		private PrintWriter output = new PrintWriter(stringWriter, true);

		public StreamConsumer(InputStream inputStream) {
			this.inputStream = inputStream;
			new Thread(this).start();
		}

		public void run() {
			try {
				InputStreamReader isr = new InputStreamReader(inputStream);
				BufferedReader br = new BufferedReader(isr);
				while (true) {
					String s = br.readLine();
					if (s == null)
						break;
					output.println(s);
				}
				inputStream.close();
				output.close();
			} catch (Exception e) {
				throw new Defect("problems reading input stream", e);
			}
		}

		@Override
		public String toString() {
			return stringWriter.toString();
		}
	}
}
